package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dao.FamilyDao;
import org.example.entity.Family;
import org.example.entity.FamilyDetail;
import org.example.entity.User;
import org.example.exception.AccountExitException;
import org.example.exception.PermissionsDenyException;
import org.example.service.FamilyDetailService;
import org.example.service.FamilyService;
import org.example.service.UserService;
import org.example.vo.ChildVo;
import org.example.vo.RegisterVo;
import org.example.utils.TimeUtils;
import org.example.vo.ParentAndStudents;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.example.exception.BizCodeEnum.ACCOUNT_EXIT;
import static org.example.interceptor.LoginInterceptor.threadLocal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FamilyServiceImpl extends ServiceImpl<FamilyDao,Family> implements FamilyService {

    @Autowired
    UserService userService;
    @Autowired
    FamilyService familyService;
    @Autowired
    FamilyDetailService familyDetailService;
    @Override
    @Transactional
    public User createFamily(RegisterVo registerVo, User parent) throws AccountExitException, PermissionsDenyException {
        if (threadLocal.get().isChild==1) throw new PermissionsDenyException();
        //先去重
        User account = userService.getOne(new QueryWrapper<User>().eq("account", registerVo.account));
        if(account!=null) throw new AccountExitException();

        //先创建一个孩子
        User child = new User(registerVo,1);
        userService.changePasswordAndSave(child);

        //如果该家长已经创建家庭了，那就不新建家庭，如果没有，新建家庭
        FamilyDetail familyDetail = familyDetailService.getOne(new QueryWrapper<FamilyDetail>().eq("user_id", parent.id));
        if(familyDetail==null){
            Date currentTime = TimeUtils.getCurrentTime();
            Family family = new Family(currentTime);
//            试了发现 familyId会直接回填的
            familyService.save(family);
            familyDetail = new FamilyDetail(currentTime, family.id  , parent.id, parent.name);
            familyDetail.setRole(1);
            familyDetailService.save(familyDetail);
        }
            //家庭详情表已经存在了该家长
        familyDetail = familyDetailService.getOne(new QueryWrapper<FamilyDetail>().eq("user_id", parent.id));
        int familyId =  familyDetail.getFamilyId();
        Date currentTime = TimeUtils.getCurrentTime();
        familyDetail = new FamilyDetail(currentTime,familyId,child.id, child.name);
        familyDetail.setRole(0);
        familyDetailService.save(familyDetail);
        return child;
    }

    @Override
    public ParentAndStudents showFamily() {
        User cur = threadLocal.get();
        ParentAndStudents parentAndStudents = new ParentAndStudents();
        FamilyDetail familyDetail = familyDetailService.getOne(new QueryWrapper<FamilyDetail>().eq("user_id", cur.id));

        if(familyDetail==null){
            return parentAndStudents;
        }else {
            List<FamilyDetail> familyDetailList= familyDetailService.list(new QueryWrapper<FamilyDetail>().eq("family_id",familyDetail.getFamilyId()));
            parentAndStudents.childVoArrayList = new ArrayList<>();
            for (FamilyDetail fd:familyDetailList
                 ) {
                //如果是家长
                if(fd.role==1){
                    parentAndStudents.setParentId(fd.familyId);
                    parentAndStudents.setParentName(fd.name);
                }else {
                    //小孩
                    ChildVo child = new ChildVo(fd.name, fd.familyId);
                    parentAndStudents.childVoArrayList.add(child);
                }
            }
        }
        return parentAndStudents;
    }
}
